import geopandas
import numpy as np
import pandas as pd
from shapely.geometry import Point
import missingno as msn
import seaborn as sns
import matplotlib.pyplot as plt
% matplotlib inline
new=pd.read_csv("data/Police Facility Locations.csv")
new
import json
def convert(o):
if isinstance(o, np.int64): return int(o)
raise TypeError
json.dumps({'value': np.int64(42)}, default=convert)
import folium
import pandas as pd
SF_COORDINATES = (43.7001100,-79.4163000)
state_geo = geopandas.read_file("data/Neighbourhoods.GeoJson")
crimedata=pd.read_csv("data/MCI_2014_to_2019 - Copy.csv")
MAX_RECORDS = 1000
mapp = folium.Map(location=SF_COORDINATES, tiles='Stamen Terrain', zoom_start=12)
popup_text = "{}<br> Faclity name: {}<br> Address: "
crimedata2 = pd.DataFrame(crimedata['Neighbourhood'].value_counts().astype(int))
crimedata2 = crimedata2.reset_index()
crimedata2.columns = ['Neighbour', 'Number']
folium.Choropleth(
geo_data=state_geo,
#data_out = 'crimeagg.json',
data = crimedata2,
columns = ['Neighbour', 'Number'],
key_on = 'feature.id',
fill_color = 'BuPu',
fill_opacity = 0,
line_opacity = 0.2,
legend_name = 'Number of incidents per Neibourhood').add_to(mapp)
folium.GeoJson(state_geo, name='geojson').add_to(mapp)
for index, each in new[0:MAX_RECORDS].iterrows():
popup_text = popup_text.format(each["FACILITY_NAME"],
each["ADDRESS"])
folium.Marker(
location = [each['LAT'],each['LONG']],
popup=popup_text,icon=folium.Icon(color='red'),
clustered_marker = True).add_to(mapp)
folium.Circle(
location = [each['LAT'],each['LONG']],radius=5000).add_to(mapp)
mapp.save('mappolice.html')
crimedata2
crimedata.info()
crime=pd.read_csv("data/MCI_2014_to_2019.csv")
crime.info()
new_mci=['Assault','Auto Theft','Break and Enter']
crime=crime[crime.MCI.isin(new_mci)]
crime
crime.info()
crime["datetime"] = pd.to_datetime(crime["occurrencedate"])
crime["datetime2"] = pd.to_datetime(crime["reporteddate"])
crime['year'] = crime['datetime'].dt.year
crime['hour'] = crime['datetime'].dt.hour
crime['Delay']=crime["datetime2"]-crime["datetime"]
crime['Delay']
crime=crime[crime['Delay']<=datetime.timedelta(days=1)]
crime
crimea2 = pd.DataFrame(crime['Delay'].value_counts().astype(int))
crimea2 = crimea2.reset_index()
crimea2.columns = ['Delay', 'Number']
crimea2
crimea2= crimea2[crimea2['Number']>1]
crimea2= crimea2[crimea2['Delay']<=datetime.timedelta(days=1)]
crimea2= crimea2[crimea2['Delay']>=datetime.timedelta(minutes=1)]
crimea2.head(10)
(crimea2['Number']).sum()
import datetime
new2014= crime[crime['year']==2014]
new2014= new2014[new2014['Delay']>=datetime.timedelta(minutes=6)]
new2014
new2015= crime[crime['year']==2015]
new2015= new2015[new2015['Delay']>=datetime.timedelta(minutes=6)]
new2015
new2016= crime[crime['year']==2016]
new2016= new2016[new2016['Delay']>=datetime.timedelta(minutes=5)]
new2016
new2017= crime[crime['year']==2017]
new2017= new2017[new2017['Delay']>=datetime.timedelta(minutes=5)]
new2017
new2018= crime[crime['year']==2018]
new2018= new2018[new2018['Delay']>=datetime.timedelta(minutes=5)]
new2018
new2019= crime[crime['year']==2019]
new2019= new2019[new2019['Delay']>=datetime.timedelta(minutes=5)]
new2019
n2014=new2014.event_unique_id.count()
n2015=new2015.event_unique_id.count()
n2016=new2016.event_unique_id.count()
n2017=new2017.event_unique_id.count()
n2018=new2018.event_unique_id.count()
n2019=new2019.event_unique_id.count()
mapp = folium.Map(location=SF_COORDINATES, tiles='Stamen Terrain', zoom_start=12)
popup_text = "{}<br> Faclity name: {}<br> Address: "
state_geo = geopandas.read_file("data/Neighbourhoods.geojson")
crimedata2 = pd.DataFrame(crimedata['Neighbourhood'].value_counts().astype(int))
crimedata2 = crimedata2.reset_index()
crimedata2.columns = ['Neighbour', 'Number']
folium.Choropleth(geo_data=state_geo,fill_opacity=-5,fill_color='red',
line_weight=1).add_to(mapp)
folium.GeoJson(state_geo, name='geojson').add_to(mapp)
for index, each in new2014[0:n2014].iterrows():
folium.Marker(
location = [each['Lat'],each['Long']],
icon=folium.Icon(icon='cloud'),
clustered_marker = True).add_to(mapp)
for index, each in new[0:20].iterrows():
folium.Circle(
location = [each['LAT'],each['LONG']],colour="0A8A9F",radius=2500).add_to(mapp)
mapp.save('mappolice1.html')
new_mci=['Assault']
new2019=new2019[new2019.MCI.isin(new_mci)]
new2019= new2019[new2019['Delay']<=datetime.timedelta(days=1)]
new2019= new2019[new2019['Delay']<=datetime.timedelta(hours=1)]
new2019
mapp = folium.Map(location=SF_COORDINATES, tiles='openstreetmap', zoom_start=12)
popup_text = "{}<br> Faclity name: {}<br> Address: "
#popup_text = popup_text.format(new["FACILITY_NAME"],
# new["ADDRESS"])
state_geo = geopandas.read_file("data/Neighbourhoods.geojson")
crimedata2 = pd.DataFrame(crimedata['Neighbourhood'].value_counts().astype(int))
crimedata2 = crimedata2.reset_index()
crimedata2.columns = ['Neighbour', 'Number']
# add a marker for every record in the filtered data, use a clustered view
folium.Choropleth(geo_data=state_geo,fill_opacity=-5,fill_color='red',
line_weight=1).add_to(mapp)
folium.GeoJson(state_geo, name='geojson').add_to(mapp)
def color(mci):
if mci<=datetime.timedelta(minutes=15):
col='green'
elif mci<=datetime.timedelta(minutes=30):
col='orange'
else:
col='red'
return col
for index, each in new[0:20].iterrows():
folium.Circle(location = [each['LAT'],each['LONG']],colour="red",radius=2500).add_to(mapp)
for lat,lon,name,fac in zip(new['LAT'],new['LONG'],new['ADDRESS'],new['FACILITY_NAME']):
folium.Marker(location=[lat,lon],popup=(folium.Popup(fac)),icon_color='red').add_to(mapp)
for lat,lon,name,delay in zip(new2019['Lat'],new2019['Long'],new2019['Neighbourhood'],new2019['Delay']):
folium.Marker(location=[lat,lon],popup=(folium.Popup(name)),icon=folium.Icon(color=color(delay),icon_color='red')).add_to(mapp)
legend_html = '''
<div style="position: fixed;
bottom: 100px; left: 100px; width: 280px; height: 160px;
border:2px solid grey; z-index:9999; font-size:17px,color:green;
"> Legend <br>
less than 15 M <i class="fa fa-map-marker fa-2x" style="color:green"></i><br>
lesss than 30 M <i class="fa fa-map-marker fa-2x" style="color:orange"></i><br>
greaterthan 30 m <i class="fa fa-map-marker fa-2x" style="color:red"></i>
</div>
'''
mapp.get_root().html.add_child(folium.Element(legend_html))
mapp.save('policeradius.html')
mapp = folium.Map(location=SF_COORDINATES, tiles='openstreetmap', zoom_start=12)
popup_text = "{}<br> Faclity name: {}<br> Address: "
#popup_text = popup_text.format(new["FACILITY_NAME"],
# new["ADDRESS"])
state_geo = geopandas.read_file("data/Neighbourhoods.geojson")
crimedata2 = pd.DataFrame(crimedata['Neighbourhood'].value_counts().astype(int))
crimedata2 = crimedata2.reset_index()
crimedata2.columns = ['Neighbour', 'Number']
# add a marker for every record in the filtered data, use a clustered view
folium.Choropleth(geo_data=state_geo,fill_opacity=-5,fill_color='red',
line_weight=1).add_to(mapp)
folium.GeoJson(state_geo, name='geojson').add_to(mapp)
for index, each in new[0:20].iterrows():
#popup_text = popup_text.format(each["FACILITY_NAME"],
# each["ADDRESS"])
folium.Circle(
location = [each['LAT'],each['LONG']],colour="0A8A9F",radius=2500).add_to(mapp)
for lat,lon,name,fac in zip(new['LAT'],new['LONG'],new['ADDRESS'],new['FACILITY_NAME']):
folium.Marker(location=[lat,lon],popup=(folium.Popup(fac)),icon_color='green').add_to(mapp)
mapp.save('policeradius.html')
state_geo
m = folium.Map([43, -100], zoom_start=4)
folium.Choropleth(
geo_data=geopandas.read_file("data/Neighbourhoods.geojson"),data = crimedata2,
columns = ['Neighbour', 'Number'],
fill_color='BuPu',
fill_opacity=0.3,
line_weight=2,legend_name = 'Number of incidents per Neibourhood',
).add_to(m)
m.save('GeoJSON_and_choropleth_7.html')
# PLOTING ALTERNATIVE
# COPied FROM THE PREVIOUS PLOTING SECTIONN
country = geopandas.read_file("data/Neighbourhoods.geojson")
country.head()
florence = new2014
florence.head()
florence['coordinates'] = florence[['X', 'Y']].values.tolist()
florence.head()
florence['coordinates'] = florence['coordinates'].apply(Point)
florence = geopandas.GeoDataFrame(florence, geometry='coordinates')
florence.head()
florence.plot(figsize=(20,10));
# Plotting to see the hurricane overlay the US map:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')
# plotting the hurricane position on top with red color to stand out:
florence.plot(ax=base, color='Black', marker="*", markersize=100);
cam=new
cam
cam['coordinates'] = cam[['LONG', 'LAT']].values.tolist()
cam.head()
cam['coordinates'] = cam['coordinates'].apply(Point)
cam.head()
cam = geopandas.GeoDataFrame(cam, geometry='coordinates')
cam.head()
# Plotting to see the hurricane overlay the US map:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')
# plotting the hurricane position on top with red color to stand out:
florence.plot(ax=base, color='Black', marker="*", markersize=500);
# div around toronto
cam.plot(ax=base, color='Red', marker="o", markersize=5000);